//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Verifications.Implementations
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using Model;

    public class WebGetVerification : VerificationBase<WebGetVerificationDescription>
    {
        private IEnumerable<HttpStatusCode> acceptedStatusCodes;

        private IEnumerable<HttpStatusCode> AcceptedStatusCodes
        {
            get
            {
                if (this.acceptedStatusCodes == null)
                {
                    var noCodesProvides = this.Description.AcceptableStatusCodes == null ||
                                          this.Description.AcceptableStatusCodes.Any();

                    this.acceptedStatusCodes = noCodesProvides
                                                   ? new[] { HttpStatusCode.OK }
                                                   : this.Description.AcceptableStatusCodes;
                }

                return this.acceptedStatusCodes;
            }
        }

        protected override bool OnBeforeVerification(VerificationResult result)
        {
            const string SettingName = "Url";
            Func<Uri> convert = () => new Uri(this.Description.GetUrl());

            return this.Description.GetUrl.SuccessfullyYieldsValue(SettingName, result) &&
                   convert.SuccessfullyYieldsValue(SettingName, result);
        }

        protected override void AddVerificationDetail(VerificationResult result)
        {
            result.AddMessage(MessageType.Information, "Attempting to connect to: {0}", this.Description.GetUrl());
            foreach (var statusCode in this.AcceptedStatusCodes)
            {
                result.AddMessage(MessageType.Information, "Acceptable status code: {0}", statusCode);
            }
        }

        protected override void ExecuteVerification(VerificationResult result)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(this.Description.GetUrl());

            if (this.Description.OnBeforeRequestSend != null)
            {
                this.Description.OnBeforeRequestSend(webRequest);
            }

            using (var response = (HttpWebResponse)webRequest.GetResponse())
            {
                if (!this.AcceptedStatusCodes.Contains(response.StatusCode))
                {
                    result.AddMessage(MessageType.Error, "Unexpected status code returned: {0}", response.StatusCode);
                }
            }
        }
    }
}
